package vip.ola.code.ctrl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.ola.code.common.ctrl.BaseController;
import vip.ola.core.common.util.MD5Util;
import vip.ola.core.common.vo.PowerVO;

import java.util.List;

@RestController
@RequestMapping("/api/notify")
public class NotifyController extends BaseController {

    private final String key = "eFNO6nBwNTZCW0Vpornzv79oQiGMltwO";

    private final Logger logger = LoggerFactory.getLogger(NotifyController.class);

    @RequestMapping("/power")
    public String power(String code, String data, String sendDate, String sign){
        logger.info(code);
        logger.info(data);
        logger.info(sendDate);
        logger.info(sign);
        if(!"200".equals(code)){
            return "success";
        }
        // 加密规则：把data中数组字符串拼接上key，然后再进行md5
        if(StringUtils.isBlank(data)){
            return "success";
        }
        // 例如：[{"logNo":"202005090190185405","xxx":"xxx"},{...}]eFNO6nBwNTZCW0Vpornzv79oQiGMltwO
        String md5Str = MD5Util.string2MD5(data + key);
        if(!sign.equalsIgnoreCase(md5Str)){
            return "fail";
        }
        List<PowerVO> dataList = JSONArray.parseArray(data, PowerVO.class);
        // todo: 1.去重，2.确认订单为成功
        for (PowerVO vo : dataList){
            System.out.println(vo.toString());
        }
        return "success";
    }
}
